package com.weipt.juc.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author peter.wei 2020/09/10 16:22
 * @version 1.0
 * @company CBI
 * @description <br>peter.wei 2020/09/10 16:22 8锁问题
 * 谁调用方法锁谁，方法分为静态和非静态
 * 非静态锁对象，静态锁类的模板
 * 问题1：当同一对象公用同一把锁的时候，一旦获取到锁，其他线程阻塞，直到unlock
 **/
public class Lock8_1 {
    public static void main(String[] args) {
        Test test = new Test();
        new Thread(()->{test.call();}).start();
        new Thread(()->{test.sms();}).start();
    }
}



class Test{
    Lock lock = new ReentrantLock();
    public void call(){
        lock.lock();
        try {
            TimeUnit.SECONDS.sleep(1);
            System.out.println(Thread.currentThread().getName()+"=>打电话");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void sms(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"=>发短信");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}